package main

import (
	"fmt"
	"log"

	"github.com/pgparser/pkg/parser"
)

func main() {
	fmt.Println("PostgreSQL Parser Example")
	fmt.Println("========================")

	// 测试简单的SELECT语句
	testCases := []string{
		"SELECT id, name FROM users;",
		"SELECT * FROM products WHERE price > 100;",
		"SELECT u.name, p.title FROM users u JOIN posts p ON u.id = p.user_id;",
		"INSERT INTO users (name, email) VALUES ('John', 'john@example.com');",
		"UPDATE users SET name = 'Jane' WHERE id = 1;",
		"DELETE FROM users WHERE id = 1;",
	}

	for i, sql := range testCases {
		fmt.Printf("\nTest Case %d: %s\n", i+1, sql)
		fmt.Println("---")
		
		// 解析SQL
		ast, err := parser.ParseSQLToAST(sql)
		if err != nil {
			log.Printf("Error parsing SQL: %v", err)
			continue
		}
		
		// 打印AST类型
		switch stmt := ast.(type) {
		case *parser.SelectStmt:
			fmt.Printf("Parsed as SelectStmt with %d targets\n", len(stmt.Targets))
			if len(stmt.From) > 0 {
				fmt.Printf("From table: %s\n", stmt.From[0].Name)
			}
		case *parser.InsertStmt:
			fmt.Printf("Parsed as InsertStmt into table: %s\n", getTableName(stmt.Table))
		case *parser.UpdateStmt:
			fmt.Printf("Parsed as UpdateStmt for table: %s\n", getTableName(stmt.Table))
		case *parser.DeleteStmt:
			fmt.Printf("Parsed as DeleteStmt from table: %s\n", getTableName(stmt.Table))
		default:
			fmt.Printf("Parsed as unknown statement type: %T\n", stmt)
		}
	}
}

// getTableName 从TableRef中提取表名
func getTableName(tableRef *parser.TableRef) string {
	if tableRef == nil {
		return "<unknown>"
	}
	return tableRef.Name
}